﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using BCEntity.OA.Attendance.AttendanceLocation;
using BCCommon;

namespace BCData.OA.Attendance.AttendanceLocation
{
    public class AttendanceLocationData : IAttendanceLocationData
    {
        private const string QUERY = @"SELECT 
	                                    ModeId,
                                        GroupId,
	                                    LocationId,
	                                    LocationName,
	                                    Address,
	                                    Longitude,
	                                    Latitude,
	                                    EffectiveScope,
	                                    AdminId,
	                                    AdminName,
	                                    CreateTime,ModifierId,
	                                    ModifierName,
	                                    ModifyTime,
	                                    Status
                                    FROM
	                                    OA_AttendanceLocation  WHERE Status<>'Abandon'";

        private readonly IDatabaseContext databaseContext;

        public AttendanceLocationData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        /// <summary>
        /// 校验考勤地址信息存在不
        /// </summary>
        /// <param name="locationId">地址Id</param>
        /// <returns></returns>
        public async Task<bool> CheckIsExist(long locationId)
        {
            string sql = @"SELECT 1 FROM OA_AttendanceLocation WHERE LocationId=?LocationId  AND Status<>'Abandon' LIMIT 1";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?LocationId", locationId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameter) != null;
        }

        /// <summary>
        /// 删除打卡地址
        /// </summary>
        /// <param name="locationId">地址Id</param>
        /// <param name="adminName">管理员信息</param>
        /// <returns></returns>

        public async Task<int> DeleteAsync(long locationId, string adminName = null)
        {
            string sql = SqlCommands.GetUpdateSql("OA_AttendanceLocation", new string[] { "Status", "ModifierName", "ModifyTime" }, new string[] { "LocationId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?LocationId", locationId, DbType.Int64);
            parameters.Add("?Status", "Abandon", DbType.AnsiString, size: 50);
            parameters.Add("?ModifierName", adminName, DbType.AnsiString, size: 50);
            parameters.Add("?ModifyTime", DateTime.Now, DbType.DateTime);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction);
        }

        /// <summary>
        /// 获取考勤打卡地址信息列表
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<AttendanceLocationEntity>> GetAsync()
        {
            return await this.databaseContext.Slave.QueryAsync<AttendanceLocationEntity>(string.Format("{0}", QUERY));
        }

        /// <summary>
        /// 获取打卡地址信息
        /// </summary>
        /// <param name="locationId">地址Id</param>
        /// <returns></returns>
        public async Task<AttendanceLocationEntity> GetEntityAsync(long locationId)
        {
            string sql = string.Format("{0} AND LocationId=?LocationId;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?LocationId", locationId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<AttendanceLocationEntity>(sql, parameters);
        }

        /// <summary>
        /// 新创建一个考勤打卡地址信息
        /// </summary>
        /// <param name="entity">打卡地址</param>
        /// <returns></returns>
        public async Task<AttendanceLocationEntity> InsertAsync(AttendanceLocationEntity entity)
        {
            DynamicParameters parameters = new DynamicParameters();
            string sql = SqlCommands.GetInsertSql("OA_AttendanceLocation", new string[] { "LocationId", "ModeId", "GroupId", "LocationName", "Address", "Longitude", "Latitude", "EffectiveScope", "AdminId", "AdminName" });
            parameters.Add("?LocationId", entity.LocationId, DbType.Int64);
            parameters.Add("?ModeId", entity.ModeId, DbType.AnsiString);
            parameters.Add("?GroupId", entity.GroupId, DbType.Int64);
            parameters.Add("?LocationName", entity.LocationName, DbType.AnsiString, size: 100);
            parameters.Add("?Address", entity.Address, DbType.AnsiString, size: 500);
            parameters.Add("?Longitude", entity.Longitude, DbType.Double);
            parameters.Add("?Latitude", entity.Latitude, DbType.Double);
            parameters.Add("?EffectiveScope", entity.EffectiveScope, DbType.Double);
            parameters.Add("?AdminId", entity.AdminId, DbType.Int64);
            parameters.Add("?AdminName", entity.AdminName, DbType.AnsiString, size: 30);

            string sqlQuery = string.Format("{0}{1} AND LocationId=LAST_INSERT_ID();", sql, QUERY);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<AttendanceLocationEntity>(sqlQuery, parameters, databaseContext.Transaction);
        }

        /// <summary>
        /// 修改打卡地址信息
        /// </summary>
        /// <param name="entity">打卡地址</param>
        /// <returns></returns>
        public async Task<AttendanceLocationEntity> UpdateAsync(AttendanceLocationEntity entity)
        {
            DynamicParameters parameters = new DynamicParameters();
            string sql = SqlCommands.GetUpdateSql("OA_AttendanceLocation", new string[] { "LocationName", "Address", "Longitude", "Latitude", "EffectiveScope", "LocationName", "ModifierName", "ModifyTime", "ModifierId" }, new[] { "LocationId", });
            string sqlQuery = string.Format("{0}{1} AND LocationId=?LocationId;", sql, QUERY);
            parameters.Add("?LocationId", entity.LocationId, DbType.Int64);
            parameters.Add("?LocationName", entity.LocationName, DbType.AnsiString, size: 100);
            parameters.Add("?Address", entity.Address, DbType.AnsiString, size: 500);
            parameters.Add("?Longitude", entity.Longitude, DbType.Double);
            parameters.Add("?Latitude", entity.Latitude, DbType.Double);
            parameters.Add("?EffectiveScope", entity.EffectiveScope, DbType.Double);
            parameters.Add("?ModifierId", entity.ModifierId, DbType.Int64);
            parameters.Add("?ModifierName", entity.AdminName, DbType.AnsiString, size: 30);
            parameters.Add("?ModifyTime", DateTime.Now, DbType.DateTime);

            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<AttendanceLocationEntity>(sqlQuery, parameters, databaseContext.Transaction);
        }

        /// <summary>
        /// 获取打卡地址
        /// </summary>
        /// <param name="modeId">打卡方式</param>
        /// <param name="groupId">考勤组Id</param>
        /// <returns></returns>
        public async Task<IEnumerable<AttendanceLocationEntity>> GetListByModeIdAndGroupIdAsync(long groupId)
        {
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GroupId", groupId, DbType.Int64);
            return await this.databaseContext.Slave.QueryAsync<AttendanceLocationEntity>(string.Format("{0} AND GroupId=?GroupId", QUERY), parameters);
        }
    }
}
